//
//  AlignCollectionViewController.m
//  ObjectiveCTools
//
//  Created by aidong on 2021/10/28.
//  Copyright © 2021 姬友大人. All rights reserved.
//

#import "AlignCollectionViewController.h"
#import "AlignCollectionViewFlowLayout.h"
#import "AlignCollectionViewHeader.h"
#import "AlignCollectionViewCell.h"

#pragma mark - 模型
@interface AlignCollectionModel : NSObject
@property (nonatomic,copy) NSString *content;
@property (nonatomic,assign) CGSize size;
@end
@implementation AlignCollectionModel
@end


#pragma mark - 控制器
@interface AlignCollectionViewController () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong) UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation AlignCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self addContentView];
    //    [self performSelector:@selector(setSome) withObject:nil afterDelay:2];
}

- (void)addContentView {
    AlignCollectionViewFlowLayout * flowLayout = [[AlignCollectionViewFlowLayout alloc]initWithType:AlignWithLeft betweenOfCell:5.0];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = UIColor.brownColor;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[AlignCollectionViewHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"h"];
    [self.collectionView registerClass:[AlignCollectionViewCell class] forCellWithReuseIdentifier:@"c"];
}

- (void)loadData {
    self.dataArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < 100; i++) {
        NSInteger n = arc4random() % 10 + 1;
        AlignCollectionModel *model = [[AlignCollectionModel alloc] init];
        model.content = [NSString stringWithFormat:@"%d",i];
        model.size = CGSizeMake((n * 5) + 50,30);
        [self.dataArray addObject:model];
    }
}

-(void)setSome{
    [self.dataArray removeAllObjects];
    [self.collectionView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        self.collectionView.frame = self.view.frame;
        [self.collectionView reloadData];
    } completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        
    }];
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
#pragma mark -- UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count / 2;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AlignCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"c" forIndexPath:indexPath];
    AlignCollectionModel *model = [self.dataArray objectAtIndex:[indexPath item]];
    cell.content = model.content;
    return cell;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader){
        AlignCollectionViewHeader * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"h" forIndexPath:indexPath];
        [headerView updateLabelFrame];
        reusableview = headerView;
    }
    return reusableview;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(collectionView.frame.size.width, 40);
}
#pragma mark --UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    AlignCollectionModel *model = [self.dataArray objectAtIndex:[indexPath row]];
    return model.size;
}


@end
